<template>
  <div class="english-quiz-container">
    <header class="quiz-header">
      <h1>英语填空题练习</h1>
      <p class="quiz-description">请在每个句子的空白处填入合适的单词，完成后点击提交检查答案</p>
    </header>

    <main class="quiz-content">
      <form @submit.prevent="handleSubmit"
            @keydown.enter.prevent class="quiz-form">
        <!-- 题目列表 -->
        <div class="questions-container">
          <div
              v-for="(question, index) in questions"
              :key="index"
              class="question-item"
              :class="{ 'correct': question.isCorrect, 'incorrect': question.isIncorrect }"
          >
            <div class="question-number">问题 {{ index + 1 }}:</div>

            <div class="question-sentence">
              <!-- 句子片段和填空框 -->
              <template v-for="(part, partIndex) in question.sentenceParts" :key="partIndex">
                <!-- 文本片段 -->
                <span v-if="typeof part === 'string'" class="sentence-text">{{ part }}</span>

                <!-- 填空输入框 -->
                <span v-else class="blank-container">
                  <input
                      type="text"
                      v-model="question.userAnswer"
                      :placeholder="part.placeholder"
                      :disabled="isSubmitted"
                      class="blank-input"
                      @input="handleInput(index)"
                  >
                  <!-- 正确答案提示（提交后显示） -->
                  <span v-if="isSubmitted && !question.isCorrect" class="correct-answer">
                    正确答案: {{ question.correctAnswer }}
                  </span>
                </span>
              </template>
            </div>

            <!-- 提交后的反馈 -->
            <div v-if="isSubmitted" class="feedback">
              <span v-if="question.isCorrect" class="feedback-correct">
                <i class="icon-check"></i> 正确!
              </span>
              <span v-if="question.isIncorrect" class="feedback-incorrect">
                <i class="icon-times"></i> 不正确
              </span>
            </div>
          </div>
        </div>

        <!-- 按钮区域 -->
        <div class="button-group">
          <button
              type="submit"
              class="submit-btn"
              :disabled="isSubmitted"
          >
            提交答案
          </button>

          <button
              type="button"
              class="reset-btn"
              @click="resetQuiz"
          >
            重新开始
          </button>

          <button
              v-if="false"
              type="button"
              class="show-answer-btn"
              @click="showAllAnswers"
              :disabled="!isSubmitted"
          >
            查看所有答案
          </button>
        </div>

        <!-- 得分显示 -->
        <div v-if="isSubmitted" class="score-display">
          <h3>得分: {{ score }}/{{ questions.length }}</h3>
          <p class="score-percentage">{{ Math.round((score / questions.length) * 100) }}%</p>
        </div>
      </form>
    </main>
  </div>
</template>

<script setup>
import {ref, computed} from 'vue';

// 题目数据
const questions = ref([
  {
    sentenceParts: [
      "She ",
      {placeholder: "动词"},
      " to school every day."
    ],
    correctAnswer: "goes",
    userAnswer: "",
    isCorrect: false,
    isIncorrect: false
  },
  {
    sentenceParts: [
      "We ",
      {placeholder: "助动词"},
      " going to the park tomorrow."
    ],
    correctAnswer: "are",
    userAnswer: "",
    isCorrect: false,
    isIncorrect: false
  },
  {
    sentenceParts: [
      "He has ",
      {placeholder: "数量词"},
      " books in his bag."
    ],
    correctAnswer: "many",
    userAnswer: "",
    isCorrect: false,
    isIncorrect: false
  },
  {
    sentenceParts: [
      "This is ",
      {placeholder: "物主代词"},
      " pen."
    ],
    correctAnswer: "my",
    userAnswer: "",
    isCorrect: false,
    isIncorrect: false
  },
  {
    sentenceParts: [
      "They ",
      {placeholder: "动词过去式"},
      " to the cinema last night."
    ],
    correctAnswer: "went",
    userAnswer: "",
    isCorrect: false,
    isIncorrect: false
  }
]);

// 状态管理
const isSubmitted = ref(false);
const showAllAnswersState = ref(false);

// 处理输入
const handleInput = (index) => {
  // 清除之前的判断状态
  questions.value[index].isCorrect = false;
  questions.value[index].isIncorrect = false;
};

// 提交答案
const handleSubmit = () => {
  // 检查每个答案
  questions.value.forEach(question => {
    // 忽略大小写和前后空格
    const isCorrect = question.userAnswer.trim().toLowerCase() ===
        question.correctAnswer.toLowerCase();
    question.isCorrect = isCorrect;
    question.isIncorrect = !isCorrect;
  });

  isSubmitted.value = true;
  showAllAnswersState.value = false;
};

// 重置测验
const resetQuiz = () => {
  questions.value.forEach(question => {
    question.userAnswer = "";
    question.isCorrect = false;
    question.isIncorrect = false;
  });

  isSubmitted.value = false;
  showAllAnswersState.value = false;
};

// 显示所有答案
const showAllAnswers = () => {
  showAllAnswersState.value = true;
};

// 计算得分
const score = computed(() => {
  return questions.value.filter(q => q.isCorrect).length;
});
</script>

<style scoped>
.english-quiz-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
  color: #333;
  line-height: 1.6;
}

.quiz-header {
  text-align: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 2px solid #42b983;
}

.quiz-header h1 {
  color: #2c3e50;
  margin-bottom: 10px;
}

.quiz-description {
  color: #666;
  font-size: 16px;
}

.questions-container {
  margin-bottom: 30px;
}

.question-item {
  background-color: #f9f9f9;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 15px;
  transition: all 0.3s ease;
}

.question-item.correct {
  border-left: 4px solid #42b983;
  background-color: #f0fff4;
}

.question-item.incorrect {
  border-left: 4px solid #e74c3c;
  background-color: #fff5f5;
}

.question-number {
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 10px;
  font-size: 16px;
}

.question-sentence {
  font-size: 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.sentence-text {
  white-space: pre-wrap;
}

.blank-container {
  position: relative;
}

.blank-input {
  width: 120px;
  padding: 8px 12px;
  border: 2px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s ease;
  text-align: center;
}

.blank-input:focus {
  outline: none;
  border-color: #42b983;
  box-shadow: 0 0 0 2px rgba(66, 185, 131, 0.2);
}

.blank-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.correct-answer {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  color: #e74c3c;
  font-style: italic;
}

.feedback {
  margin-top: 10px;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 14px;
}

.feedback-correct {
  color: #2ecc71;
  display: flex;
  align-items: center;
  gap: 5px;
}

.feedback-incorrect {
  color: #e74c3c;
  display: flex;
  align-items: center;
  gap: 5px;
}

.button-group {
  display: flex;
  justify-content: center;
  gap: 15px;
  margin-bottom: 30px;
  flex-wrap: wrap;
}

.submit-btn, .reset-btn, .show-answer-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.submit-btn {
  background-color: #42b983;
  color: white;
}

.submit-btn:hover:not(:disabled) {
  background-color: #359e6d;
  transform: translateY(-2px);
}

.submit-btn:disabled {
  background-color: #a5d6b9;
  cursor: not-allowed;
}

.reset-btn {
  background-color: #3498db;
  color: white;
}

.reset-btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
}

.show-answer-btn {
  background-color: #f39c12;
  color: white;
}

.show-answer-btn:hover:not(:disabled) {
  background-color: #d35400;
  transform: translateY(-2px);
}

.show-answer-btn:disabled {
  background-color: #f8c471;
  cursor: not-allowed;
}

.score-display {
  text-align: center;
  padding: 20px;
  background-color: #f0f7ff;
  border-radius: 8px;
  margin-top: 20px;
}

.score-display h3 {
  color: #2c3e50;
  margin-bottom: 10px;
}

.score-percentage {
  font-size: 24px;
  font-weight: bold;
  color: #42b983;
}

/* 图标样式 */
.icon-check::before {
  content: "✓";
}

.icon-times::before {
  content: "✗";
}

/* 响应式设计 */
@media (max-width: 600px) {
  .question-sentence {
    font-size: 16px;
  }

  .blank-input {
    width: 100px;
  }

  .button-group {
    flex-direction: column;
    align-items: center;
  }

  .submit-btn, .reset-btn, .show-answer-btn {
    width: 100%;
    max-width: 300px;
  }
}
</style>
    